package com.quanshu.exchange.ui.gui.pay;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;

import com.alipay.sdk.app.PayTask;
import com.quanshu.change.broadcast.PayResultBroadcatReciver;
import com.quanshu.exchange.support.utils.ToastHelper;
import com.quanshu.exchange.ui.base.BaseActivity;
import com.quanshu.exchange.ui.gui.profile.AllOrderActivity;

import java.util.Map;

/**
 * Created by jye on 2018/2/3.
 */

public class AlipayActivity extends BaseActivity {

    private static final int SDK_PAY_FLAG = 1;
    private String payInfo = "";
    public static final String PAGE_STATE = "page_state";
    private int state = 0;

    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler() {
        @SuppressWarnings("unused")
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case SDK_PAY_FLAG: {
                    @SuppressWarnings("unchecked")
                    PayResult payResult = new PayResult((Map<String, String>) msg.obj);
                    /**
                     对于支付结果，请商户依赖服务端的异步通知结果。同步通知结果，仅作为支付结束的通知。
                     */
                    String resultInfo = payResult.getResult();// 同步返回需要验证的信息
                    String resultStatus = payResult.getResultStatus();
                    // 判断resultStatus 为9000则代表支付成功
                    if (TextUtils.equals(resultStatus, "9000")) {
                        // 该笔订单是否真实支付成功，需要依赖服务端的异步通知。
                        ToastHelper.toast("支付成功");
                        if (state==0){
                            Intent intent = new Intent(AlipayActivity.this, AllOrderActivity.class);
                            startActivity(intent);
                        }else {
                            Intent intent=new Intent();
                            intent.setAction(PayResultBroadcatReciver.PAY_GOLD_RECHAGR_SUCCESSFUL);
                            sendBroadcast(intent);
                        }
                        finish();
                    } else {
                        // 该笔订单真实的支付结果，需要依赖服务端的异步通知。
                        ToastHelper.toast("支付失败");
                        finish();
                    }
                    break;
                }
                default:
                    break;
            }
        }

        ;
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        state = getIntent().getIntExtra("state",0);
        initIntent();
    }


    private void initIntent() {
        String url = getIntent().getStringExtra("url");
        initData(url);
    }

    private void initData(String url) {
        Log.d("支付宝", "initData: " + url);
        if (url != null && url.length() > 0) {
            String[] split = url.split("[?]");
            if (split != null) {
                payInfo = split[1].substring(13);
                doPay();
            }

        }

    }

    private void doPay() {
        Runnable payRunnable = () -> {
            PayTask alipay = new PayTask(AlipayActivity.this);
            Map<String, String> result = alipay.payV2(payInfo, true);

            Message msg = new Message();
            msg.what = SDK_PAY_FLAG;
            msg.obj = result;
            mHandler.sendMessage(msg);
        };
        // 必须异步调用
        Thread payThread = new Thread(payRunnable);
        payThread.start();
    }

}
